import BaseComponent from "../../../../../app/baseComponent";

import { OrderTraits } from "../../traits/orders";

export class KxAppCrmOrdersearch extends BaseComponent {
    constructor(name) {
        super(name);
        this.props = {
            value: {
                required: true
            }
        }
    }

    _template() {
        var self = this;
        return `<div class="${self._name}">
   
   
       <div class="${self._name}__searchbar">
    <input class="aui-form-control" type="text"
        v-model="private_value" @input="private_onChange"><button 
        class="aui-button aui-button--default" 
        @click="private_search" type="button" :disabled="private_checkError()">搜索</button>
</div> 
   
   
<div class="${self._name}__result" v-if="items.length > 0">
<div class="${self._name}__info"><div class="${self._name}__info-item" v-for="item in items">{{item.key}}:{{item.value}}</div>
</div><button 
    class="aui-button aui-button--default ${self._name}__onkeygo" @click="private_onkeygo" type="button">一键填写</button>
</div>

<div class="${self._name}__nodata" v-if="items.length < 1 && private_firstsearch ">
没有查询到相关订单
</div>

</div>
`;
    }

    _methods() {
        var methods = {};
        methods.private_onChange = function () {
            var self = this;
            self.$emit("input", self.private_value);
        };
        methods.private_checkError = function () {
            var self = this;
            return !self.private_value;
        };
        methods.private_search = function () {
            var self = this;
            if (!self.private_firstsearch) {
                self.private_firstsearch = true;
            }
            self.private_traits_orders.send({
                order_no: self.private_value,
                product_id: 0
            });
        };

        methods.private_onkeygo = function () {
            var self = this;
            self.$emit("onkeygo", {
                item: self.private_items
            });
        };
        methods.reset = function () {
            var self = this;
            self.items = [];
            self.private_value = "";
            self.private_firstsearch = false;
        };
        return methods;
    }

    _traits(instance) {
        return [
            new OrderTraits(instance)
        ]
    }

    _mounted() {
        var self = this;
        self.private_value = self.value;

        self.private_traits_orders.init();
        self.private_traits_orders.cache = false;
        self.private_traits_orders.onLoad = function (options) {
            if (options.length > 0) {
                self.private_items = options[0];
                if (self.onSetItems) {
                    self.items = self.onSetItems(self.private_items);
                }
            } else {
                self.items = [];
            }
        };
    }

    _data() {
        var ret = {};
        ret.private_value = "";
        ret.private_firstsearch = false;
        ret.private_items = {};
        ret.items = [];
        return ret;
    }
}

KxAppCrmOrdersearch.install = function () {
    window.AUICustomComponents.define("kx-app-crm-ordersearch", KxAppCrmOrdersearch);
};